package com.szzg10.webservice.chapter5;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

public class TempConvertRClient {

	public static void main(String[] args) throws XPathExpressionException {
		QName service_name = new QName("TempConvert");
		QName port_name = new QName("TempConvertPort");
		String endpoint = "http://localhost:8080/szzg10/tcauth";
		
		Service service = Service.create(service_name);
		service.addPort(port_name, HTTPBinding.HTTP_BINDING, endpoint);
		
		Dispatch<Source> dispatch = service.createDispatch(port_name, Source.class, Service.Mode.PAYLOAD);
		
		dispatch.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "fred");
		dispatch.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "rockbed");
		
		dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, "GET");
		dispatch.getRequestContext().put(MessageContext.QUERY_STRING, "-40.1");
		
		StreamSource streamSource = (StreamSource)dispatch.invoke(null);
		InputSource inputSource = new InputSource(streamSource.getInputStream());
		
		String expression = "//object";
		XPath xPath = XPathFactory.newInstance().newXPath();
		String list = xPath.evaluate(expression, inputSource);
		
		System.out.println(list);
	}
}
